package com.dudu.log.voiceLog.dao;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.mortbay.log.Log;

import core.CoreDao;
import core.db.DBConnectionUtil;

/**
 * 语音通知dao
 * 
 * @author jack
 *
 */
public class VoiceDao {

	/**
	 * 查询条数（分页用）
	 * 
	 * @param paramSql
	 * @return
	 */
	public int getTotalAcount(CoreDao dao, String paramSql) {
		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuilder sql = new StringBuilder();
			sql.append("select count(0) as totalAcount from t_send_notice ");
			if (StringUtils.isNotBlank(paramSql)) {
				sql.append("where ").append(paramSql);
			}

			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] {});

			JSONObject obj = list.get(0);
			result = (Integer) obj.get("totalAcount");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	/**
	 * 查询数据（分页）
	 * 
	 * @param paramSql
	 * @param pageSql
	 * @return
	 */
	public List<JSONObject> queryAll(CoreDao dao, String paramSql, String pageSql) {
		Connection con = null;
		List<JSONObject> result = null;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuilder sql = new StringBuilder();
			sql.append(
					"select id,displayNumber,session_id,voice_type,callee,batch_number,status,cust_account,meet_ims_id,schedule_send_time,create_time from t_send_notice ");
			if (StringUtils.isNotBlank(paramSql)) {
				sql.append("where ").append(paramSql);
			}
			sql.append(" order by create_time desc ").append(pageSql);
			result = dao.serviceJsonSql(con, sql.toString(), new Object[] { null });
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	public int resendVoiceById(CoreDao dao, String id, String sendTime) {
		Connection con = null;
		int count = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuilder sql = new StringBuilder();
			sql.append("update t_send_notice set STATUS=?,run_thread=?,send_count=? where id in(");
			sql.append("?");
			sql.append(") and  (status=? or status=?) ");
			List params = new ArrayList();
			params.add("-1");
			params.add("");
			params.add("0");
			params.add(id);
			params.add("1");
			params.add("3");
			List<JSONObject> result = dao.serviceJsonSql(con, sql.toString(), params.toArray());
			JSONObject obj = result.get(0);
			count = obj.optInt("result");
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return count;
	}
	
	public int resendVoiceByParam(CoreDao dao, String param, String sendTime) {
		Connection con = null;
		int count = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuilder sql = new StringBuilder();
			sql.append("update t_send_notice set STATUS=?,run_thread=?,send_count=? where ");
			sql.append(" (status=? or status=?) ");
			sql.append("  and " + param );
			List params = new ArrayList();
			params.add("-1");
			params.add("");
			params.add("0");
			params.add("1");
			params.add("3");
			List<JSONObject> result = dao.serviceJsonSql(con, sql.toString(), params.toArray());
			JSONObject obj = result.get(0);
			count = obj.optInt("result");
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return count;
	}

}
